package com.ruoyi.web.wrapper;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.ruoyi.common.wrapper.BaseControllerWrapper;
import org.apache.catalina.security.SecurityUtil;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

public class SourceDataCountInfoWrapper extends BaseControllerWrapper {

    public SourceDataCountInfoWrapper(Map<String, Object> single) {
        super(single);
    }

    public SourceDataCountInfoWrapper(List<Map<String, Object>> multi) {
        super(multi);
    }

    // 方法用于计算比率并将结果存入map
    private void calculateRate(Map<String, Object> map, String key, BigDecimal numerator, BigDecimal denominator) {
        if (ObjectUtil.isAllNotEmpty(numerator, denominator) && denominator.compareTo(BigDecimal.ZERO) != 0) {
            // 计算比率并保留四位小数
            BigDecimal rate = numerator.divide(denominator, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
            // 将结果存入 map 中，保留两位小数并添加百分号
            map.put(key, String.format("%.2f%%", rate));
        } else {
            map.put(key, "0%");
        }
    }


    @Override
    public void wrapTheMap(Map<String, Object> map) {
        try {
            if (map != null && !map.isEmpty()) {

                // 获取各项数据
                BigDecimal approveSuccessNumber = getBigDecimalValue(map.get("approveSuccessNumber"));
                BigDecimal checkNumber = getBigDecimalValue(map.get("checkNumber"));
                BigDecimal clickApproveNumber = getBigDecimalValue(map.get("clickApproveNumber"));
                BigDecimal checkPassNumber = getBigDecimalValue(map.get("checkPassNumber"));
                BigDecimal registerPassNumber = getBigDecimalValue(map.get("registerPassNumber"));
                BigDecimal openApproveNumber = getBigDecimalValue(map.get("openApproveNumber"));
                BigDecimal lessRate = (BigDecimal) map.get("lessRate");


                // 检查空值
                if (ObjectUtil.isAllNotEmpty(approveSuccessNumber, clickApproveNumber, checkPassNumber, registerPassNumber, openApproveNumber)) {
                    // 计算注册率
                    calculateRate(map, "zhuangkuRate", checkPassNumber, checkNumber);
                    calculateRate(map, "zhuceRate", registerPassNumber, checkPassNumber);

                    // 计算打开率
                    calculateRate(map, "dakaiRate", openApproveNumber, registerPassNumber);

                    // 计算点击率
                    calculateRate(map, "dianjiRate", clickApproveNumber, openApproveNumber);

                    // 计算成功率
                    calculateRate(map, "successRate", approveSuccessNumber, clickApproveNumber);
                } else {
                    map.put("zhuceRate", "0%");
                    map.put("dakaiRate", "0%");
                    map.put("dianjiRate", "0%");
                    map.put("successRate", "0%");
                    map.put("zhuangkuRate", "0%");
                }

                // 计算结算数
                if (ObjectUtil.isAllNotEmpty(approveSuccessNumber, lessRate)) {
                    BigDecimal settleRate = BigDecimal.ONE.subtract(lessRate);
                    BigDecimal shouldNoticeNumber = approveSuccessNumber.multiply(settleRate).setScale(0, RoundingMode.DOWN);
                    map.put("shouldNoticeNumber", shouldNoticeNumber);
                    map.put("settleRate", settleRate);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    // 方法用于将对象转换为BigDecimal
    private BigDecimal getBigDecimalValue(Object obj) {
        if (obj instanceof BigDecimal) {
            return (BigDecimal) obj;
        } else if (obj instanceof Integer) {
            return new BigDecimal((Integer) obj);
        } else if (obj instanceof String) {
            try {
                return new BigDecimal((String) obj);
            } catch (NumberFormatException e) {
                // 如果转换失败，返回0
                return BigDecimal.ZERO;
            }
        } else {
            // 其他情况返回0
            return BigDecimal.ZERO;
        }
    }
}
